//
//  NSString+SelfSize.m
//  Mamahome
//
//  Created by AQ on 15-4-21.
//  Copyright (c) 2015年 OIT. All rights reserved.
//

#import "NSString+SelfSize.h"

@implementation NSString (SelfSize)
- (CGSize)getSizeFromSelfWithWidth:(CGFloat)width andFont:(CGFloat)font
{
    CGFloat fontT = font;
    if(font<1)
        fontT = 17.;
    UIFont *fontTemp = [UIFont systemFontOfSize:fontT];
    NSDictionary *attribute = @{NSFontAttributeName:fontTemp};
    CGSize labelSize = [self boundingRectWithSize:CGSizeMake(width, 999) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;
    return labelSize;
}

- (CGSize)getSizeFromSelfWithWidth:(CGFloat)width andUIFont:(UIFont *)font
{
    UIFont *fontTemp = font;
    if(!font)
        fontTemp = [UIFont systemFontOfSize:17.];
    NSDictionary *attribute = @{NSFontAttributeName:fontTemp};
    CGSize labelSize = [self boundingRectWithSize:CGSizeMake(width, 999) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;
    return labelSize;
}

- (NSInteger)getNewLength
{
    NSInteger lengthTemp = 0;
    for (int i = 0; i<self.length; i++) {
        NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
        if([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]>1)
        {
            lengthTemp += 2;
        }
        else
            lengthTemp +=1;
    }
    
    return lengthTemp;
}

- (NSInteger)isValidString
{
    NSInteger lengthTemp = 0;
    for (int i = 0; i<self.length; i++) {
        NSString *str = [self substringWithRange:NSMakeRange(i, 1)];
        NSInteger leTemp = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
        if(leTemp == 3)
        {
            lengthTemp += 1;
        }
        else if(leTemp == 2 || leTemp>3)
        {
            lengthTemp = 0;
            break;
        }
        else
        {
            if ([str isEqualToString:@" "]) {
                lengthTemp += 1;
            }
            else
            {
                NSInteger strValue = [str characterAtIndex:0];
                if ((strValue>=65&&strValue<=90)||(strValue>=97 &&strValue<=122)) {
                    lengthTemp +=1;
                }
                else
                {
                    lengthTemp = 0;
                    break;
                }
            }
        }

    }
    
    return lengthTemp;
}

@end
